/**
 * Created by ${PRODUCT_NAME}.
 * User: skylerparr
 * Date: 2/2/11
 * Time: 9:08 AM
 * To change this template use File | Settings | File Templates.
 */
package com.thoughtorigin.display.containers {
import flash.display.DisplayObject;
import flash.events.EventDispatcher;

public class DefaultLayoutContainer extends EventDispatcher implements LayoutContainer {

    private var _container: Container;
    private var _layoutPolicy: String = LayoutPolicy.HORIZONTAL_LAYOUT;
    private var _gap: Number = 0;
    private var _cellWidth: Number;
    private var _cellHeight: Number;
    private var _overwritePlacement: Boolean;

    public function DefaultLayoutContainer(value: Container) {
        _container = value;
        _container.addEventListener(ContainerEvent.RAW_CHILDREN_CHANGE, onRawChildrenChange, false, 0, true);
    }

    private function onRawChildrenChange(ce: ContainerEvent):void {
        refresh();
    }

    public function set layoutPolicy(value:String):void {
        if(value == LayoutPolicy.HORIZONTAL_LAYOUT || value == LayoutPolicy.VERTICAL_LAYOUT) {
            _layoutPolicy = value;
            refresh();
        }
    }

    public function get layoutPolicy(): String {
        return _layoutPolicy;
    }

    public function get overwritePlacement():Boolean {
        return _overwritePlacement;
    }

    public function set overwritePlacement(value:Boolean):void {
        _overwritePlacement = value;
    }

    public function set gap(value:Number):void {
        if(value != _gap) {
            _gap = value;
            refresh();
        }
    }

    public function get gap():Number {
        return _gap;
    }

    public function get cellWidth():Number {
        return _cellWidth;
    }

    public function set cellWidth(value:Number):void {
        if(value != _cellWidth) {
            _cellWidth = value;
            refresh();
        }
    }

    public function get cellHeight():Number {
        return _cellHeight;
    }

    public function set cellHeight(value:Number):void {
        if(value != _cellHeight) {
            _cellHeight = value;
            refresh();
        }
    }

    public function refresh():void {
        if(_layoutPolicy == LayoutPolicy.HORIZONTAL_LAYOUT) {
            refreshHorizontalLayout();
        } else if(_layoutPolicy == LayoutPolicy.VERTICAL_LAYOUT) {
            refreshVerticalLayout()
        }

        dispatchEvent(new LayoutEvent(LayoutEvent.LAYOUT_REFRESH));
        _container.recalculateBounds();
    }

    private function refreshHorizontalLayout(): void {
        var xIndex: Number = 0;

        for(var i: int = 0; i < _container.rawChildContainer.numChildren; i++) {
            var displayObj: DisplayObject = _container.rawChildContainer.getChildAt(i);
            displayObj.x = xIndex;
            if(_overwritePlacement) {
                displayObj.y = 0;
            }
            if(isNaN(_cellWidth)) {
                xIndex += displayObj.width + _gap;
            } else {
                xIndex += _cellWidth + _gap;
            }
        }
    }

    private function refreshVerticalLayout(): void {
        var yIndex: Number = 0;
        for(var i: int = 0; i < _container.rawChildContainer.numChildren; i++) {
            var displayObj: DisplayObject = _container.rawChildContainer.getChildAt(i);
            displayObj.y = yIndex;
            if(_overwritePlacement) {
                displayObj.x = 0;
            }
            if(isNaN(_cellHeight)) {
                yIndex += displayObj.height + _gap;
            } else {
                yIndex += _cellHeight + _gap;
            }
        }

    }
}
}
